<template>
  <div class="login">
    <!-- 顶部 -->
    <van-nav-bar @click-left="$router.back()">
      <template #left>
        <i class="iconfont icon-sdf" style="fontSize: 18px"></i>
      </template>
    </van-nav-bar>
    <h3>您好，请登录</h3>
    <van-form @submit="onSubmit">
      <!-- 输入框 -->
      <van-cell-group class="input">
        <van-field
          v-model="user.mobile"
          placeholder="请输入手机号码"
          :rules="rules.mobile"
        >
          <template #left-icon>
            <i class="iconfont icon-phone" style="fontSize:22px"></i>
          </template>
        </van-field>
        <van-field
          v-model="user.code"
          placeholder="请输入验证码"
          :rules="rules.code"
        >
          <template #left-icon>
            <i
              class="iconfont icon-codev2"
              style="fontSize:18px;marginLeft:5px"
            ></i>
          </template>
          <template #button>
            <span class="code" v-if="totalTime !== 60"
              >{{ totalTime }}s后重试</span
            >
            <span class="code" v-else @click="getCode">获取验证码</span>
          </template>
        </van-field>
      </van-cell-group>
      <!-- 用户协议 -->
      <p class="agreement">
        登录即同意 <span class="blue">《用户使用协议》</span> 和<span
          class="blue"
          >《隐私协议》</span
        >
      </p>
      <!-- 登录按钮 -->
      <van-button type="danger" round block>确定</van-button>
    </van-form>
  </div>
</template>

<script>
import { getCode, login } from '@/api/api.js'
import { setLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      user: {
        mobile: '18888881111',
        code: ''
      },
      // 验证规则
      rules: {
        mobile: [
          { required: true, trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确的手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, trigger: 'onBlur' },
          {
            pattern: /^\d{4}$/,
            message: '请输入4位数字的验证码',
            trigger: 'onBlur'
          }
        ]
      },
      // 发送验证码的倒计时
      totalTime: 60,
      //   获取的用户信息
      userInfo: ''
    }
  },
  methods: {
    //   获取验证码
    async getCode () {
      // 判断手机号正确则发送验证码
      const pattern = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
      if (pattern.test(this.user.mobile)) {
        // 手机验证正确，发送请求
        this.$toast.loading()
        const res = await getCode({ mobile: this.user.mobile })
        this.$toast.success(res.data.data)
        // 开启获取验证码的定时器
        this.totalTime--
        var timeout = setInterval(() => {
          this.totalTime--
          if (this.totalTime === 0) {
            clearInterval(timeout)
            this.totalTime = 60
          }
        }, 1000)
      } else {
        this.$toast.fail('请输入正确的手机号')
      }
    },
    //   表单提交
    async onSubmit () {
      // 发送用户登录的请求
      const res = await login(this.user)
      this.userInfo = res.data.data
      this.$router.push('/my')
      this.$toast.success('欢迎')
      console.log(res)
      setLocal(res.data.data.jwt, 'heimamm_mobile_token')
      var userInfo = res.data.data.user
      userInfo.avatar = process.env.VUE_APP_URL + userInfo.avatar
      this.$store.commit('SETUSERINFO', userInfo)
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  padding: 10px;
  h3 {
    line-height: 80px;
    margin-left: 10px;
  }
  .input {
    .code {
      padding: 3px 8px;
      font-size: 16px;
      color: #4fcde1;
      border-left: 1px solid #f7f8f9;
    }
  }
  /deep/.van-hairline--top-bottom::after,
  .van-hairline-unset--top-bottom::after {
    border-top: none;
  }
  .agreement {
    font-size: 12px;
    padding-left: 10px;
    margin-bottom: 50px;
    .blue {
      color: #4fcde1;
    }
  }
}
</style>
